﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace OverNANA
{
    public class DownLoadCommon
    {
        public static string GetHttp(string Url, string PostType, string codes)
        {
            string StrHttp = "";
            if (PostType == "POST")
            {
                string[] TmpUrl = Url.Split('?');
                string PostStr = TmpUrl[TmpUrl.Length - 1];
                byte[] requestBytes = System.Text.Encoding.Default.GetBytes(PostStr);
                System.Net.HttpWebRequest httpReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(TmpUrl[0]);
                httpReq.Timeout = 10000;//设置超时值2秒
                httpReq.Method = "POST";
                httpReq.ContentType = "application/x-www-form-urlencoded";
                httpReq.ContentLength = requestBytes.Length;
                Stream requestStream = httpReq.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();

                System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)httpReq.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(codes));
                StrHttp = sr.ReadToEnd();
                sr.Close();
                res.Close();
                sr = null;
                res = null;
            }
            else
            {
                System.Net.HttpWebRequest httpReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);//HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持'，也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。
                httpReq.ContentType = "application/x-www-form-urlencoded";
                //httpReq.Headers.Add("Accept-Language", "zh-cn");
                httpReq.Timeout = 10000; //设置超时值10秒
                httpReq.Method = "GET";
                System.Net.HttpWebResponse httpResq = (System.Net.HttpWebResponse)httpReq.GetResponse();
                StreamReader reader = new StreamReader(httpResq.GetResponseStream(), System.Text.Encoding.GetEncoding(codes));//如是中文，要设置编码格式为“GB2312”。
                string respHTML = reader.ReadToEnd(); //respHTML就是页面源代码
                StrHttp = respHTML;
                httpResq.Close();
                httpResq = null;
            }
            return StrHttp;
        }

        
    }
}
